

#import "ViewController.h"
#import "PhoneBookTableViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) UITableView *table;

@property (strong, nonatomic) NSDictionary *dictionary;

@end

@implementation ViewController
static NSString *cellIdKey=@"PhoneBookTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _table=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _table.delegate=self;
    _table.dataSource=self;
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_table];
    //注册cell
    [_table registerNib:[UINib nibWithNibName:@"PhoneBookTableViewCell" bundle:nil] forCellReuseIdentifier:cellIdKey];
    //首先读取studentInfo.plist中的数据
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Source" ofType:@"plist"];
    _dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"%@",_dictionary);
}



#pragma mark -- UITableViewDataSource and UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_dictionary allKeys].count;
}
- ( UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    UILabel *one=[[UILabel alloc]init];
    one.font=[UIFont systemFontOfSize:17.0f];
    one.textColor=[UIColor grayColor];
    one.baselineAdjustment=UIBaselineAdjustmentAlignCenters;
    NSString *str=[[_dictionary allKeys] objectAtIndex:section];
    
    one.text=[NSString stringWithFormat:@"    %@",str];
    one.backgroundColor=[UIColor yellowColor];
    
    return one;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PhoneBookTableViewCell *recordCell=[_table dequeueReusableCellWithIdentifier:cellIdKey];
    return recordCell.frame.size.height;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr=[_dictionary objectForKey:[[_dictionary allKeys] objectAtIndex:section ]];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PhoneBookTableViewCell *recordCell=[_table dequeueReusableCellWithIdentifier:cellIdKey];
    NSDictionary *dic=[[_dictionary objectForKey:[_dictionary.allKeys objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    
    recordCell.phoneNumLab.text=dic[@"phone"];
    recordCell.titleLab.text=dic[@"name"];
    
    recordCell.selectionStyle=UITableViewCellSelectionStyleNone;
    return recordCell;
}


//tableview的headview不停靠的方法，重载scrollview的delegate方法，或者将tableviewstyle设置成group
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
